package com.cloudkinto.controller.zone;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.ft.FreightStatusUpdateReq;
import com.cloudkinto.vo.zone.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.zone.ZoneTempService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 分区规则 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-11-01
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "分区规则omp模块")
@RequestMapping("/api/admin/omp/ZoneTemp")
public class ZoneTempController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ZoneTempController.class);

    private final ZoneTempService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "分区规则新增初始化", notes = "分区规则新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "分区规则新增", notes = "分区规则新增")
    public SingleResult add(@RequestBody @Validated ZoneTempAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功", service.add(req, userId, getTenantId()));
    }

//    @GetMapping("/updateInit/{id}")
//    @ApiOperation(value = "分区规则编辑初始化", notes = "分区规则编辑初始化")
//    public SingleResult updateInit(@PathVariable("id") Long id) {
//        return service.updateInit(id);
//    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "分区规则修改", notes = "分区规则修改")
    public SingleResult update(@RequestBody @Validated ZoneTempUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId, getTenantId()));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "分区规则根据id删除", notes = "分区规则根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "分区规则根据id查看详情", notes = "分区规则根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ZoneTempDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "分区规则查询初始化", notes = "分区规则查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "分区规则查询", notes = "分区规则查询")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ZoneTempPageRes.class))
    public SingleResult list(HttpServletRequest request) {

        Map<String, String> map = new HashMap<>();
        String tenantId = request.getParameter("tenantId");
        map.put("tenantId", tenantId);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "分区规则查询（分页）", notes = "分区规则查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "zoneTempName", value = "规则名称"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ZoneTempPageRes.class))
    public PageResult listPage(HttpServletRequest request) {

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String zoneTempName = request.getParameter("zoneTempName");
        String tenantId = request.getParameter("tenantId");
        map.put("tenantId", tenantId);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("zoneTempName", zoneTempName);
        return service.getListPage(map);
    }


    @GetMapping("/testZone")
    @ApiOperation(value = "测试分区", notes = "测试分区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "city", value = "城市"),
            @ApiImplicitParam(name = "id", value = "分区规则id"),
            @ApiImplicitParam(name = "postcode", value = "邮编"),
            @ApiImplicitParam(name = "countryCode", value = "国家/地区"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ZoneTempTestRes.class))
    public SingleResult testZone(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        String city = request.getParameter("city");
        String id = request.getParameter("id");
        String postcode = request.getParameter("postcode");
        String countryCode = request.getParameter("countryCode");
        map.put("city", city);
        map.put("id", id);
        map.put("postcode", postcode);
        map.put("countryCode", countryCode);
        map.put("zoneType", "zoneTemp");
        return SingleResult.success("查询成功", service.testZone(map));
    }


    @UserSessionRequired
    @PostMapping("/importT")
    @ApiOperation(value = "导入分区规则")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ZoneTempImportRes.class))
    public SingleResult importT(MultipartFile file) {
        ZoneTempImportRes zoneTempImportRes = service.importT(file);
        return SingleResult.success(zoneTempImportRes);
    }


    @UserSessionRequired
    @PutMapping("/updateStatus")
    @ApiOperation(value = "物流费停用启用", notes = "物流费停用启用")
    public SingleResult updateStatus(@RequestBody @Validated FreightStatusUpdateReq req) {
        Long userId = getCurrentUserId();
        service.updateStatus(req, userId, getTenantId());
        return SingleResult.success();
    }


}

